package com.bgs.controller;

import com.bgs.pojo.*;
import com.bgs.service.FragmentService;
import com.bgs.tool.R;
import com.google.common.net.InetAddresses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
@Slf4j
@RestController
public class FragmentController {
    @Autowired
    FragmentService fragmentService;
    @Autowired
    RedisTemplate redisTemplate;
    /*日程开始*/
    //    删除
    @PostMapping("/rcdelete")
    public R rcdelete(Integer sid) {
        boolean d = fragmentService.rcdelete(sid);
        return R.ok().data("data", d);
    }
    //    添加
    @PostMapping("/rcAdd")
    public R rcAdd(@RequestBody ScheduleVo scheduleVo) {
        boolean b = fragmentService.rcAdd(scheduleVo);
        return R.ok().data("data", b);
    }
    @PostMapping("/findAllDateRc")
    public R findAllDateRc(@RequestBody TimeTest timeTest){
        return fragmentService.findAllDateRc(timeTest.getStarttime());
    }
    @PostMapping("/findRC")
    public ScheduleVo findRC(Integer sid) {
        return fragmentService.findRC(sid);
    }
    //    查看所有日程
    @PostMapping("/xgzt")
    public R xgzt(@RequestBody  ScheduleVo scheduleVo) {
        boolean b=fragmentService.xgzt(scheduleVo);
        return R.ok().data("data",b);
    }
//    查看所有日程
    @PostMapping("/findAllRc")
    public R findAllRc(String sname) {
        return fragmentService.findAllRc(sname);
    }
    /*日程结束*/
    /*文档开始*/
    //    查看我的文档
    @PostMapping("/findMyWd")
    public R findMyWd(String submitter) {
        return fragmentService.findMyWd(submitter);
    }

    //    查看所有文档
    @PostMapping("/findallwd")
    public R findallwd(DocxVo docxVo) {
        return fragmentService.findallwd(docxVo);
    }

    //    添加
    @PostMapping("/addWD")
    public R addWD(@RequestBody DocxVo docxVo) {
        boolean b = fragmentService.addWD(docxVo);
        return R.ok().data("data", b);
    }

    /*文档结束*/
    /*公告收藏*/
    //    添加
    @PostMapping("/addGGSc")
    public R addGGSc(@RequestBody NoticcollectVo noticcollectVo) {
        boolean b = fragmentService.addGGSc(noticcollectVo);
        return R.ok().data("data", b);
    }

    //    删除
    @PostMapping("/deleteGGSc")
    public R deleteGGSc(Integer noticeid) {
        boolean b = fragmentService.deleteGGSc(noticeid);
        return R.ok().data("data", b);
    }

    /*公告收藏结束*/
    /*新闻收藏开始*/
//    删除
    @PostMapping("/deleteXwSc")
    public R deleteXwSc(Integer jourid) {
        boolean d = fragmentService.deleteXwSc(jourid);
        return R.ok().data("data", d);
    }

    //    添加
    @PostMapping("/addXwSc")
    public R addXwSc(@RequestBody JourcollectVo jourcollectVo) {
        boolean b = fragmentService.addXwSc(jourcollectVo);
        return R.ok().data("data", b);
    }

    /*新闻收藏结束*/
    /*新闻表*/
    @PostMapping("/findxw0")
    R findxw0(JournalismVo journalismVo) {
        return fragmentService.findxw0(journalismVo);
    }

    @PostMapping("/findxw1")
    R findxw1(JournalismVo journalismVo) {
        return fragmentService.findxw1(journalismVo);
    }

    @PostMapping("/findxw2")
    R findxw2(JournalismVo journalismVo) {
        return fragmentService.findxw2(journalismVo);
    }

    //  展示所有新闻
    @PostMapping("/hfindAllXinwen")
    public R hfindAllXinwen(JournalismVo journalismVo) {
        return fragmentService.hfindAllXinwen(journalismVo);
    }

    //   新闻详情
    @PostMapping("/findxwID")
    public JournalismVo findxwID(Integer jourid) {
        if (redisTemplate.hasKey("xinwen" + jourid)) {
            redisTemplate.boundValueOps("xinwen" + jourid).increment();
        } else {
            redisTemplate.boundValueOps("xinwen" + jourid).set(1);
        }
        int num = (int) redisTemplate.boundValueOps("xinwen" + jourid).get();
        JournalismVo list = fragmentService.findxwID(jourid);
        list.setNum(num);
        list.setJourid(jourid);
        int i1 = fragmentService.updateNum(list);

        return fragmentService.findxwID(jourid);
    }

    //    新闻名称搜索
    @PostMapping("/findXinWenName")
    public R findXinWenName(String theme) {
        return fragmentService.findXinWenName(theme);
    }

    /*新闻表结束*/
    /*公告表*/
    //查询指定公告根据ID
    @PostMapping("/findgongaoId")
    public NoticeVo findgongaoId(Integer noticeid) throws UnknownHostException {
        if (redisTemplate.hasKey("gonggao" + noticeid)) {
            redisTemplate.boundValueOps("gonggao" + noticeid).increment();
        } else {
            redisTemplate.boundValueOps("gonggao" + noticeid).set(1);
        }
        int sum = (int) redisTemplate.boundValueOps("gonggao" + noticeid).get();

        NoticeVo list = fragmentService.findgongaoId(noticeid);
        list.setSum(sum);
        list.setNoticeid(noticeid);
        int i = fragmentService.updategongaoSum(list);
        return fragmentService.findgongaoId(noticeid);
    }

    //   展示所有公告
    @PostMapping("/hSelectAllgongao")
    public R hSelectAllgongao(NoticeVo noticeVo) {
        return fragmentService.hSelectAllgongao(noticeVo);
    }

    //    公告搜索
    @PostMapping("/findGonggaoName")
    public R findGonggaoName(String noticename) {
        return fragmentService.findGonggaoName(noticename);
    }
    /*公告表结束*/
}
